package com.tudouji.project.bill.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.common.utils.poi.ExcelTempUtil;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.bill.domain.vo.ProduceItemRep;
import com.tudouji.project.bill.domain.vo.ProducePlanReportVO;
import com.tudouji.project.bill.domain.vo.ProduceRep;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.mapper.StoreroomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.bill.mapper.ProducereportMapper;
import com.tudouji.project.bill.domain.Producereport;
import com.tudouji.project.bill.service.IProducereportService;

/**
 * 生产报Service业务层处理
 * 
 * @author tudouji
 * @date 2021-03-01
 */
@Service
public class ProducereportServiceImpl implements IProducereportService 
{
    @Autowired
    private ProducereportMapper producereportMapper;
    @Autowired
    private StoreroomMapper storeroomMapper;
    @Autowired
    private ExcelTempUtil excelTempUtil;
    @Autowired
    private ISalespermissionService salespermissionService;

    /**
     * 查询生产报
     * 
     * @param id 生产报ID
     * @return 生产报
     */
    @Override
    public Producereport selectProducereportById(Long id)
    {
        return producereportMapper.selectProducereportById(id);
    }

    /**
     * 查询生产报列表
     * 
     * @param producereport 生产报
     * @return 生产报
     */
    @Override
    public List<Producereport> selectProducereportList(Producereport producereport)
    {
        return producereportMapper.selectProducereportList(producereport);
    }
    @Override
    public AjaxResult export(Producereport producereport)
    {
        Map<String, Object> map = new HashMap<>();
//        if (producereport.getStarttime() == null){
//            producereport.setStarttime(DateUtils.getNowDay());
//        }
//        if (producereport.getEndtime() == null){
//            producereport.setEndtime(DateUtils.getNowDay());
//        }
        if (StringUtils.isNotEmpty(producereport.getSrguid())){
            Storeroom storeroom = storeroomMapper.selectStoreroomByGuid(producereport.getSrguid());
            map.put("storeroom", storeroom.getName());
        }
        List<ProduceItemRep> ylList = producereportMapper.yl(producereport);
        List<ProduceItemRep> flList = producereportMapper.fl(producereport);

        map.put("starttime", producereport.getStarttime());
        map.put("endtime", producereport.getEndtime());
        map.put("ylList", ylList);
        map.put("flList", flList);
        return excelTempUtil.exportWhoutSC(map);
    }

    /**
     * 查询所有带明细生产报列表
     *
     * @param producereport 生产报
     * @return 生产报集合
     */
    @Override
    public List<Producereport> selectProducereportListAll(Producereport producereport){
        return this.producereportMapper.selectProducereportListAll(producereport);
    }


    /**
     * 查询生产报列表
     *
     * @param producereport 生产报
     * @return 生产报
     */
    @Override
    public List<Producereport> selectPRData(Date startdate)
    {
        return producereportMapper.selectPRData(startdate);
    }

    /**
     * 新增生产报
     * 
     * @param producereport 生产报
     * @return 结果
     */
    @Override
    public int insertProducereport(Producereport producereport)
    {
        return producereportMapper.insertProducereport(producereport);
    }

    /**
     * 修改生产报
     * 
     * @param producereport 生产报
     * @return 结果
     */
    @Override
    public int updateProducereport(Producereport producereport)
    {
        return producereportMapper.updateProducereport(producereport);
    }

    /**
     * 批量删除生产报
     * 
     * @param ids 需要删除的生产报ID
     * @return 结果
     */
    @Override
    public int deleteProducereportByIds(Long[] ids)
    {
        return producereportMapper.deleteProducereportByIds(ids);
    }

    /**
     * 删除生产报信息
     * 
     * @param id 生产报ID
     * @return 结果
     */
    @Override
    public int deleteProducereportById(Long id)
    {
        return producereportMapper.deleteProducereportById(id);
    }
    @Override
    public int deleteAll(String starttime)
    {
        return producereportMapper.deleteAll(starttime);
    }
    @Override
    public int deleteAllItem(String starttime)
    {
        return producereportMapper.deleteAllItem(starttime);
    }

    /**
     *
     * 生产计划报表
     * @param producePlanReportVO
     * @return
     */
    public List<ProducePlanReportVO> producePlanReport(ProducePlanReportVO producePlanReportVO){
        return this.producereportMapper.producePlanReport(producePlanReportVO);
    }

    @Override
    public BigDecimal selectMaterial(String plcode) {
        return this.producereportMapper.selectMaterial(plcode);
    }

    @Override
    public void insertReportHistory(String nowStr) {
        producereportMapper.insertReportHistory(nowStr);
    }

    @Override
    public void insertReportItemHistory(String nowStr) {
        producereportMapper.insertReportItemHistory(nowStr);
    }

    @Override
    public void updateProducePlan(String planguid) {
        producereportMapper.updateProducePlan(planguid);
    }
}
